#include <cstdio>
#include <set>

using namespace std;

multiset<int> s;

int main( )
{
    int t, cas = 1, l, n, cmd, x, p;
    long long ans;
    set<int>::iterator i, j;
    bool flag;
    scanf("%d", &t);
    while ( t-- )
    {
        printf("Case %d: ", cas++);
        s.clear( );
        scanf("%d%d", &l, &n);
        p = 0;
        ans = 0;
        while ( n-- )
        {
            scanf("%d", &cmd);
            if ( cmd )
            {
                i = s.lower_bound( p );
                if ( i != s.end( ) )
                {
                    if ( i != s.begin( ) )
                    {
                        j = i;
                        j--;
                        if ( *i - p < p - *j )
                        {
                            ans += *i - p;
                            if ( p != *i )
                                flag = 1;
                            p = *i;
                            s.erase( i );
                        }
                        else if ( *i - p > p - *j )
                        {
                            ans += p - *j;
                            p = *j;
                            flag = 0;
                            s.erase( j );
                        }
                        else if ( flag )
                        {
                            ans += *i - p;
                            p = *i;
                            flag = 1;
                            s.erase( i );
                        }
                        else
                        {
                            ans += p - *j;
                            p = *j;
                            flag = 0;
                            s.erase( j );
                        }
                    }
                    else
                    {
                        ans += *i - p;
                        if ( p != *i )
                            flag = 1;
                        p = *i;
                        s.erase( i );
                    }
                }
                else if ( i != s.begin( ) )
                {
                    j = i;
                    j--;
                    ans += p - *j;
                    p = *j;
                    flag = 0;
                    s.erase( j );
                }
            }
            else
            {
                scanf("%d", &x);
                s.insert( x );
            }
        }
        printf("%lld\n", ans);
    }
    return 0;
}
